home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 265_01 / rdfd525.c < prev    next >
Text File  |  1990-02-13  |  4KB  |  151 lines

  1. /*
  2. *
  3. *    rdfd525 - ReaD Floppy Disk, (5 & 1/4 inch).
  4. *
  5. *    This routine is used as an input filter when a floppy disk can
  6. *    not be read directly.
  7. *
  8. *    This program attempts to read the entire disk, but it will block
  9. *    if there are 5120 unread bytes in a pipeline. When a program
  10. *    like cpio (1) detects its eof and terminates, this program will
  11. *    receive a hangup signal and exit without reading the entire disk.
  12. *
  13. *    The routine will facilitate reading in the following situations:
  14. *        a: The floppy has been formatted with double density
  15. *           (40 tracks) instead of quad density (80 tracks).
  16. *        b: The floppy was written on a single-sided drive.
  17. *        c: The floppy was written to use only eight of the nine
  18. *           available sectors on each track.
  19. *
  20. *    The program runs under UNIX V.
  21. *    The hardware supports a format of nine, 512 byte sectors per 
  22. *    track. IBM high capacity floppies, written 80 tracks of 15 
  23. *    sectors, are not supported.
  24. *    The diskette controller must support the track density specified.
  25. *
  26. *    The following options can be specified:
  27. *        [-q] -Quad track density. Default is double density.
  28. *        [-s] -Single sided. Default is double sided.
  29. *        [-8] - Eight sectors per track. Default is nine.
  30. *
  31. *    The pathname of the floppy disk, eg. /dev/rfd0, may be
  32. *    specified on the command line. Otherwise it is assumed to be
  33. *    stdin. Extraneous garbage at the end of the command is an error.
  34. *
  35. *    Example: rdfd525 /dev/rfd0 > temp will place the contents
  36. *    of a double-sided, IBM-compatible diskette in the floppy drive
  37. *    specified by /dev/rfd0 into the file named temp.
  38. *
  39. *    NOTE: Since a full track of a 5 1/4" floppy is only 4608 bytes
  40. *    it is probably safe to assume that there are no controllers
  41. *    without full-track buffering. Thus, it is unlikely that an
  42. *    interleave factor other than 1 exists for this medium. In any
  43. *    case it is not supported.
  44. *
  45. * c 1988 John E Van Deusen, PO BOX 9387, BOISE ID 83707, (208) 343-1865.
  46. *    All rights transfered to the C Users Group.
  47. */
  48.  
  49. #include <stdio.h>
  50. #include <fcntl.h>
  51.  
  52. #define SECTOR_SIZE    512
  53. #define CYLINDERS    40
  54. #define SECTOR_COUNT    9
  55.  
  56. void exit();
  57. long lseek();
  58. void perror();
  59.  
  60. extern int errno;
  61. extern char *optarg;
  62. extern int optind;
  63.  
  64. main (argc, argv)
  65. int argc;
  66. char *argv[];
  67. {
  68. int    index, 
  69.     head, 
  70.     ier=0, 
  71.     nread;
  72. short    n_heads=2, 
  73.     sectors=SECTOR_COUNT, 
  74.     cylinders=CYLINDERS;
  75. char    track[SECTOR_SIZE*SECTOR_COUNT];
  76.  
  77. while ((index = getopt(argc, argv, "qs8")) != EOF)
  78.     switch (index) {
  79.     case 'q': cylinders = CYLINDERS * 2;
  80.               break;
  81.     case 's': n_heads = 1;
  82.           break;
  83.     case '8': sectors = 8;
  84.           break;
  85.     case '?':     /* unknown */
  86.     default : ier++;
  87.           break;
  88.     };
  89. if (ier)
  90.     { 
  91.     (void) fprintf (stderr, "%s:usage [-q][-s][-8] [fl_dev]\n", argv[0]);
  92.     exit(-1);
  93.     }
  94. switch (argc - optind) {
  95. case 0: /* Input device not specified */
  96.     break;
  97. case 1: /* Input device specified */
  98.     if (freopen(argv[optind], "r", stdin) == NULL)
  99.         {
  100.         (void) fprintf (stderr, "%s: error in opening %s for input. ",
  101.                     argv[0], argv[optind]);
  102.         perror ("");
  103.         exit (-1);
  104.         }
  105.     break;
  106. default: (void) fprintf (stderr, "%s: extraneous argument(s). \n",argv[0]);
  107.      exit (-1);
  108.     /* NOTREACHED */
  109.     break;
  110. }
  111. for (index = 0; index < cylinders; index++) {
  112.     for (head = 0; head < n_heads; head++) {
  113.         if ((nread = read (0, track, (unsigned)sectors * SECTOR_SIZE)) == -1)
  114.         {
  115.         (void)fprintf (stderr, "%s: can't read track %d.", argv[0], index);
  116.         perror ("");
  117.         exit (errno);
  118.         }
  119.         if (nread != sectors * SECTOR_SIZE)
  120.         {
  121.         (void)fprintf (stderr, "%s: input device must be a floppy disk.\n", argv[0]);
  122.         exit (-1);
  123.         }
  124.         if (sectors != SECTOR_COUNT)
  125.         /* Skip the unused 9th sector */
  126.             if (lseek (0, SECTOR_SIZE, 1) == -1)
  127.             {
  128.             (void) fprintf (stderr, "%s: can not seek track %d. ", argv[0], index+1);
  129.             perror ("");
  130.             exit (errno);
  131.             }
  132.         if ( (ier = write(1, track, (unsigned) nread)) != nread)
  133.         {
  134.         (void) fprintf (stderr, "%s: can not write track %d. ", argv[0], ier);
  135.         if (ier == -1)
  136.             perror("");
  137.         else
  138.             (void) fprintf (stderr, "Return count = %d unexpected.\n", argv[0], ier);
  139.         exit (-1);
  140.         }
  141.     } /* for end */
  142.     if (n_heads == 1)    /* skip the track for head 2 */
  143.         if (lseek (0, sizeof(track), 1) == -1)
  144.         {
  145.         (void) fprintf (stderr, "%s: can not seek track %d.", argv[0], index + 1);
  146.         perror("");
  147.         exit (errno);
  148.         }
  149.     } /* for end */
  150. } /* main end */
  151.